package com.hkbigdata.reallywar;

import com.hkbigdata.bean.UserBehavior;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author liuanbo
 * @creat 2024-04-19-17:09
 * @see 2194550857@qq.com
 */
public class Flink01_Project_PV {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        SingleOutputStreamOperator<UserBehavior> map = env.readTextFile("input/UserBehavior.csv")
                .map(line -> {
                    String[] split = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(split[0]),
                            Long.valueOf(split[1]),
                            Integer.valueOf(split[2]),
                            split[3],
                            Long.valueOf(split[4])
                    );
                });

        map.filter(data -> "pv".equals(data.getBehavior()))
                .map(data -> {
                    return new Tuple2<String, Integer>(data.getBehavior(), 1);
                }).returns(Types.TUPLE(Types.STRING, Types.INT))
                .keyBy(data -> data.f0)
                .sum(1)
                .print();

        env.execute();
    }
}
